package com.car.action.api;

import java.math.BigDecimal;
import java.util.Map;

import com.car.action.util.CommonController;
import com.car.common.model.AppPutConfig;
import com.car.common.model.Users;
import com.car.common.model.Versions;
import com.car.common.model.VipRecord;
import com.car.common.model.WebConfig;
import com.google.common.collect.Maps;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.utils.APIJSON;
import com.utils.StringUtil;
import com.utils.Toolkit;

public class ServiceController extends CommonController {
	
	public void new_person(){
		WebConfig one = WebConfig.dao.findFirst("select * from web_config where `key` = 'new_person'");
		if (one != null) {
			setAttr("data", one);
			render("theme.html");	
		} else {
			renderText(APIJSON.send(0));
		}
	}
	
	
	 /**
     * 平台服务协议(注册条款)
     * @return mixed
     */
	public void agreement(){
		WebConfig one = WebConfig.dao.findFirst("select * from web_config where `key` = 'agreement'");
		if (one != null) {
			setAttr("data", one);
			render("theme.html");	
		} else {
			renderText(APIJSON.send(0));
		}
	}
	
	
	/**
     * 社区规则
     */
	public void square() {
		WebConfig one = WebConfig.dao.findFirst("select * from web_config where `key` = 'square'");
		if (one != null) {
			setAttr("data", one);
			render("theme.html");	
		} else {
			renderText(APIJSON.send(0));
		}
	}
	
	/**
     * 隐私条款
     */
	public void conceal() {
		WebConfig one = WebConfig.dao.findFirst("select * from web_config where `key` = 'conceal'");
		if (one != null) {
			setAttr("data", one);
			render("theme.html");	
		} else {
			renderText(APIJSON.send(0));
		}
	}
	
	/**
     * 提现规则
     */
	public void money() {
		WebConfig one = WebConfig.dao.findFirst("select * from web_config where `key` = 'money'");
		if (one != null) {
			setAttr("data", one);
			render("theme.html");	
		} else {
			renderText(APIJSON.send(0));
		}
	}
	
	public void about() {
		render("about.html");
	}
	
	public void version () {
		Versions one = Versions.dao.findFirst("select * from versions where status =1 order by id desc limit 1 ");
		Map<String, Object> result = Maps.newHashMap();
		if (one != null) {
			result.put("version", one.getAndroidVersion());
			result.put("code", new Double(Double.valueOf(one.getAndroidVersion()) * 10).intValue());
			result.put("down", one.getAndroidDown());
			result.put("desc", one.getDesc());
		}
		renderText(APIJSON.send(result));
	}
	
	public void api_host() {
		Map<String, String> result = Maps.newHashMap();
		String host_pro = "http://api.aicishi.cn/";
        String host_dev = "http://www.aicishi.cn/";
        String version =  getPara("version");
        String source =   getPara("source");  // Android or ios
        String mark =     getPara("mark");  // Android or ios
        String sql = "select * from app_put_config where status =1 and source = '" + source +"' and version = '" + version +"'";
        if ("android".equalsIgnoreCase(source)) {
        	sql = sql + " and mark ='" + mark + "'";
        }
        sql = sql + " order by id desc limit 1 ";
        AppPutConfig config = AppPutConfig.dao.findFirst(sql);
        if(config == null) {
        	result.put("host", host_pro);
        } else {
        	result.put("host", host_dev);
        }
		renderText(APIJSON.send(result));
	}
	
}
